package riteh.eventNetwork.client;

import com.google.gson.Gson;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.view.View.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;

public class ActivityEvent extends Activity {

	private Spinner spinnerPriority;
	private ArrayAdapter<CharSequence> adapterPriority;
	private EditText editTitle;
	private TextView labelDuration;
	private SeekBar seekDuration;
	private Button buttonPost;
	private Button buttonCircles;
	
	private boolean selCircles[];
	private int numOfSelCircles;
	private String circles[][];
	private int numOfCircles;
	private long userId;
	private Gson gson;
	
/* ********************** OVERRIDE FUNCTIONS ******************************/
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(C.TAG, "EVENT Activity");
		setContentView(R.layout.event);
	
	// INSTANCIRANJE OBJEKTA
		spinnerPriority = (Spinner) findViewById(R.id.eventSpinnerPriority);
		editTitle = (EditText) findViewById(R.id.eventEditTitle);
		labelDuration = (TextView) findViewById(R.id.eventLabelDuration);
		seekDuration = (SeekBar) findViewById(R.id.eventSeekDuration);
		buttonPost = (Button) findViewById(R.id.eventButtonPost);
		buttonCircles = (Button) findViewById(R.id.eventButtonCircles);
		adapterPriority = ArrayAdapter.createFromResource(this,R.array.priorities, android.R.layout.simple_spinner_item);
		gson = new Gson();
		
	// AKCIJE NA STARTU
		adapterPriority.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerPriority.setAdapter(adapterPriority);
		labelDuration.setText("20 min");
		userId = getIntent().getExtras().getLong("userId");
		
		if (userId == C.TEST_ID) {
			numOfCircles = C.NUM_OF_CIRCLES;
			selCircles = new boolean[numOfCircles];
			circles = new String[numOfCircles][3];
			circles = C.CIRCLES;
		}
		else {
			getCircles();
		}
		
		setResult(-1);
	
	// EVENT LISTENERS
		
		seekDuration.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar seekBar) {}
			
			public void onStartTrackingTouch(SeekBar seekBar) {}
			
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				String text = "";
				int m = 20 + ((int) Math.pow((double) progress,2) / 10);
				int h = m / 60;
				if (h == 0) text = m + " min";
				else text = h + " h " + m % 60 + " min";
				labelDuration.setText(text);
			}
		});
		
		buttonPost.setOnClickListener( new OnClickListener() {
			public void onClick(View v) {
				if (userId == C.TEST_ID) return;
				createEvent();
			}
		});
		
		buttonCircles.setOnClickListener( new OnClickListener() {
			public void onClick(View v) {
				selectCircles();
			}
		});	

	}
	
/* ********************** PRIVATE FUNCTIONS *******************************/
	
	private void getCircles() {
		Log.d(C.TAG,"EVENT: getCircles()");
    	RestClient client = new RestClient(getString(R.string.serverMethodGetCircles));
    	client.AddParam("userId", String.valueOf(userId));
    	
    	String result[][] = C.getMethod2(client, getString(R.string.serverMethodGetCircles));
    	
    	if (C.resultSuccess(this, result, getString(R.string.toastGetCirclesError))) {
    		numOfCircles = result.length;
			selCircles = new boolean[numOfCircles];
    		circles = new String[numOfCircles][3];
    		circles = result;
    	}
 
	}
	
	private void createEvent() {
		Log.d(C.TAG, "EVENT: createEvent()");
		
		String title = editTitle.getText().toString();
		String duration = labelDuration.getText().toString();
		String priority = spinnerPriority.getSelectedItem().toString();
		
		if (title.length() < 5) {
			C.toast(this, R.string.toastEventTitleEmpty);
			return;
		}
		
		if (numOfSelCircles == 0) {
			C.toast(this,R.string.toastEventNoCircles);
			return;
		}

		String selectedCircles[] = new String[numOfSelCircles];
		int tmp=0;
		for (int i=0; i<numOfCircles; i++) {
			if (selCircles[i]) {
				selectedCircles[tmp] = circles[i][0];
				tmp++;
			}
		}
		
		RestClient client = new RestClient(getString(R.string.serverMethodCreateEvent));
		client.AddParam("userId", String.valueOf(userId));
		client.AddParam("eventDescription", title);
		client.AddParam("eventDuration", duration);
		client.AddParam("eventPriority", priority);		
		client.AddParam("circles", gson.toJson(selectedCircles));
		
		String result = C.getMethod0(client, "createEvent");
		
		if (C.resultSuccess(this, result, getString(R.string.toastEventError))) {
			setResult(0);
			finish();
		}
	
	}
	
	private void selectCircles() {
		final CharSequence[] items;
		items = new CharSequence[numOfCircles];
		for (int i=0; i<numOfCircles; i++) items[i] = circles[i][1];
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Select circles you want to share this event with:");
		builder.setMultiChoiceItems(items, selCircles,  new OnMultiChoiceClickListener() {
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				selCircles[which] = isChecked;
				if (isChecked) numOfSelCircles++;
				else numOfSelCircles--;
			}
		});
		
		AlertDialog alert = builder.create();
		alert.show();
	}

}
